import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { PicClassifyEntity } from 'src/entities/picClassify.entity';
import { Repository } from 'typeorm';
import { GetCalssifyDto, ConfigClassifyDto } from './dto';

@Injectable()
export class PicClassifyService {
  constructor(
    @InjectRepository(PicClassifyEntity)
    private readonly picClassifyRepo: Repository<PicClassifyEntity>,
  ) {}
  async getClassifyList({ page, limit }: GetCalssifyDto): Promise<any> {
    try {
      const qb = this.picClassifyRepo.createQueryBuilder('picClassify');
      const res = await qb
        .skip((page - 1) * limit)
        .take(limit)
        .getManyAndCount();
      return { list: res[0], total: res[1] };
    } catch (e) {
      console.log(e);
      return Promise.reject(e);
    }
  }
  async configClassify({ classify_name, id }: ConfigClassifyDto) {
    try {
      const classify = !id
        ? new PicClassifyEntity()
        : await this.picClassifyRepo.findOne(id);
      classify.classify_name = classify_name;
      const res = await this.picClassifyRepo.save(classify);
      return res;
    } catch (e) {
      console.log(e);
      return Promise.reject(e);
    }
  }

  async delClassify(id: number) {
    try {
      await this.picClassifyRepo.delete(id);
    } catch (e) {
      console.log(e);
      return Promise.reject(e);
    }
  }
}
